package com.bw.controller;

import com.bw.entity.Dict;
import com.bw.entity.Employee;
import com.bw.entity.EmployeeAdd;
import com.bw.entity.EmployeeQuery;
import com.bw.mapper.EmployeeMapper;
import com.bw.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;


    @Autowired
    EmployeeMapper employeeMapper;

    @RequestMapping("/list.do")
    public String list(EmployeeQuery query, Integer pageNum, Model model) {
        // 1. 渲染下拉框
        List<Dict> deptList = employeeMapper.selectDept();
        List<Dict> groupList = employeeMapper.selectGroup();
        model.addAttribute("deptList", deptList);
        model.addAttribute("groupList", groupList);
        // 2. 分页列表查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        List<Employee> list = employeeMapper.selectList(query);
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        // 3. 查询条件不丢失
        model.addAttribute("query", query);
        return "list";
    }

    @RequestMapping("/del.do")
    public String del(String id) {
        System.out.println("删除的入参 " + id);
        employeeMapper.del(id);
        // 重定向
        return "redirect:list.do";
    }


    /**
     * 返回添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        // 1 渲染下拉框
        List<Dict> deptList = employeeMapper.selectDept();
        List<Dict> groupList = employeeMapper.selectGroup();
        model.addAttribute("deptList", deptList);
        model.addAttribute("groupList", groupList);
        return "add";
    }

    /**
     * 执行添加
     *
     * @param
     * @return
     */
    @RequestMapping("/add.do")
    public String add(@Valid EmployeeAdd employeeAdd, BindingResult result, Model model) {

        // 1. 非空验证
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            String defaultMessage = allErrors.get(0).getDefaultMessage();
            model.addAttribute("error", defaultMessage);
            // 1 渲染下拉框
            List<Dict> deptList = employeeMapper.selectDept();
            List<Dict> groupList = employeeMapper.selectGroup();
            model.addAttribute("deptList", deptList);
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //员工工号以“GF”开始，
        String num = employeeAdd.getNum();
        if (!num.startsWith("GF")) {
            model.addAttribute("error", "员工工号以“GF”开始");
            // 1 渲染下拉框
            List<Dict> deptList = employeeMapper.selectDept();
            List<Dict> groupList = employeeMapper.selectGroup();
            model.addAttribute("deptList", deptList);
            model.addAttribute("groupList", groupList);
            return "add";
        }
        //工号不能重复
        Employee employeeFromDB =   employeeMapper.selectByNum(num);
        if (employeeFromDB != null){
            model.addAttribute("error", "工号不能重复");
            // 1 渲染下拉框
            List<Dict> deptList = employeeMapper.selectDept();
            List<Dict> groupList = employeeMapper.selectGroup();
            model.addAttribute("deptList", deptList);
            model.addAttribute("groupList", groupList);
            return "add";
        }


//        Date createTime = employeeAdd.getCreateTime();
//        Date date = new Date();
//        if (createTime.after(date)){
//            model.addAttribute("error", "入职日期小于当前时间");
//            // 1 渲染下拉框
//            List<Dict> deptList = employeeMapper.selectDept();
//            List<Dict> groupList = employeeMapper.selectGroup();
//            model.addAttribute("deptList", deptList);
//            model.addAttribute("groupList", groupList);
//        }

       employeeService.add(employeeAdd);
        return "redirect:list.do";
    }
}
